
<template>
  <div>
    <Card ref="card">
      <p slot="title">评估阶段汇总- {{groupname}}</p>
      <Row :gutter="16">
        <Col span="3">
          <Select v-model="town" size="large" placeholder="选择乡镇">
            <Option
              v-for="item in townList"
              :value="item.TownName"
              :key="item.TownName"
            >{{ item.TownName }}</Option>
          </Select>
        </Col>
        <Col span="3">
          <Select v-model="personState" size="large" placeholder="所有人员">
            <Option v-for="item in stateList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="3">
          <Input v-model="pName" size="large" placeholder="姓名/身份证号/住址" />
        </Col>
        <Col span="3">
          <Checkbox v-model="isNew" size="large">
            <span>仅查新增人员</span>
          </Checkbox>
        </Col>
        <Col span="4">
          <Button @click="Search" size="large" type="primary" icon="ios-search">查询</Button>
        </Col>
        <Col span="2">
          <Button @click="InputPrintTitle" size="large" type="primary" icon="md-print">打印</Button>
        </Col>
        <Col span="2">
          <Button
            @click="Export"
            size="large"
            type="primary"
            :loading="exportLoading"
            icon="md-download"
          >导出</Button>
        </Col>
        <Col span="2">
          <Button
            @click="SyncEmplyeeSign"
            size="large"
            type="primary" 
            icon="md-refresh"
          >同步签名</Button>
        </Col>
      </Row>
    </Card>
    <iframe id="iframe-print" frameborder="0" style="display:none" scrolling="auto" :src="printSrc"></iframe>
    <Modal :mask-closable="false" v-model="modal2" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>修改评估人员信息</span>
      </p>
      <Form :model="personTemp" :label-width="80">
        <FormItem label="姓名">
          <Input type="text" v-model="personTemp.Name" size="large" />
        </FormItem>
        <FormItem label="供养方式">
          <RadioGroup v-model="personTemp.SupportType">
            <Radio :label="1" class="margin-right-10">集中</Radio>
            <Radio :label="2" class="margin-right-10">分散</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="身份证号">
          <Input v-model="personTemp.IdCard" size="large" />
        </FormItem>
        <FormItem label="民族">
          <Input type="text" v-model="personTemp.Nation" size="large" />
        </FormItem>
        <FormItem label="所属乡镇">
          <Input v-model="personTemp.GroupTown" size="large" />
        </FormItem>
        <FormItem label="户籍地">
          <Input v-model="personTemp.BirthAddress" size="large" />
        </FormItem>
        <FormItem label="地址">
          <Input v-model="personTemp.Address" size="large" />
        </FormItem>
      </Form>

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="SavePerson">保存</Button>
      </div>
    </Modal>
    <Modal :mask-closable="false" v-model="modal3" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>请输入打印标题</span>
      </p>
      <Row>
        <Input v-model="pringtTitle" size="large" placeholder="打印标题" />
      </Row>
      <br />

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="Print">确定</Button>
      </div>
    </Modal>
    <Modal :mask-closable="false" v-model="modalRemark" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>填写备注信息</span>
      </p>
      <Row>
        <Input v-model="personTemp.Remark" size="large" placeholder="备注" />
      </Row>

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="SaveRemark">保存</Button>
      </div>
    </Modal>
    <Row class="margin-top-10">
      <Table border :columns="tableTitle" :data="tableData" ref="table" :height="tableHeight"></Table>
    </Row>
    <Row class="margin-top-10">
      <Page
        :total="total"
        :current="pPage"
        :page-size="pSize"
        :page-size-opts="[50,100,300,500,1000,2000,5000]"
        @on-change="s=>{pPage=s;Search();}"
        @on-page-size-change="p=>{pSize=p;pPage=1;Search()}"
        show-total
        show-sizer
      />
    </Row>
  </div>
</template>
<script>
import excel from "@/libs/excel";
import { Message, Modal } from "view-design";
import { printUrl } from "@/libs/util";
import {
  GetGroupTown,
  SearchPerson,
  DeletePerson,
  SavePerson,
  SavePersonRemark,
  SaveActionLog,
  SyncEmplyeeSign
} from "@/api/data";
export default {
  name: "search_person",
  data() {
    return {
      printSrc: printUrl + "printlistnoresult.html",
      tableHeight: 450,
      total: 0,
      pPage: 1,
      pSize: 50,
      exportLoading: false,
      groupname: "",
      isNew: false,
      modal2: false,
      modal3: false,
      modalRemark: false,
      pringtTitle: "",
      personTemp: {},
      tableTitle: [
        {
          title: "序号",
          key: "R_INDEX",
          align: "center",
          width: 80
        },
        {
          title: "操作",
          key: "action",
          width: 100,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.personTemp = params.row;
                      this.modal2 = true;
                    }
                  }
                },
                "修改"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.DeletePerson(params.row.Gid);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        },
        {
          title: "姓名",
          key: "Name",
          align: "center",
          width: 80
        },
        {
          title: "性别",
          key: "Sex",
          align: "center",
          width: 80
        },
        {
          title: "供养类型",
          key: "SupportType",
          align: "center",
          width: 80,
          render: (h, params) => {
            if (params.row.SupportType == 1) {
              return h(
                "Tag",
                {
                  props: {
                    color: "success"
                  }
                },
                "集中"
              );
            }else  if (params.row.SupportType == 2) {
              return h(
                "Tag",
                {
                  props: {
                    color: "primary"
                  }
                },
                "分散"
              );
            }
          }
        },
        {
          title: "户籍地",
          key: "BirthAddress",
          align: "center",
          width: 100
        },
        {
          title: "住址",
          key: "Address",
          align: "center",
          minWidth: 150
        },
        {
          title: "身份证号",
          key: "IdCard",
          align: "center",
          width: 160
        },
        {
          title: "评估类型",
          key: "AssessType",
          align: "center",
          width: 80,
          render: (h, params) => {
            if (params.row.AssessInfo != null) {
              if (params.row.AssessInfo.AssessType == 1) {
                return h("div", "标准");
              } else if (params.row.AssessInfo.AssessType == 2) {
                return h("div", "标准(新)");
              } else if (params.row.AssessInfo.AssessType == 3||params.row.AssessInfo.AssessType == 4) {
                return h("div", "特困");
              } else {
                return h("div", "简易");
              }
            }
          }
        },
        {
          title: "状态",
          key: "FinalState",
          align: "center",
          width: 80,
          render: (h, rparams) => {
            if (rparams.row.FinalState == null) {
              return h(
                "Tag",
                {
                  props: {
                    color: "default"
                  }
                },
                "待评估"
              );
            } else {
              var ctype = "default";
              if (
                rparams.row.FinalState == "重度失能" ||
                rparams.row.FinalState == "失能"
              ) {
                ctype = "error";
              } else if (
                rparams.row.FinalState == "中度失能" ||
                rparams.row.FinalState == "半自理"
              ) {
                ctype = "warning";
              } else if (rparams.row.FinalState == "轻度失能") {
                ctype = "primary";
              } else if (
                rparams.row.FinalState == "能力完好" ||
                rparams.row.FinalState == "自理"
              ) {
                ctype = "success";
              }
              return h(
                "Button",
                {
                  props: {
                    type: ctype,
                    size: "small"
                  },
                  on: {
                    click: () => {
                      //简易评估
                      if (rparams.row.AssessInfo.AssessType == 0) {
                        this.$router.push({
                          name: "assess_info",
                          params: { id: rparams.row.Gid }
                        });
                      }
                      //标准评估
                      else if (rparams.row.AssessInfo.AssessType == 1) {
                        this.$router.push({
                          name: "standard_assess_info",
                          params: { id: rparams.row.Gid }
                        });
                      }//新标准评估
                      else if (rparams.row.AssessInfo.AssessType == 2) {
                          this.$router.push({
                            name: "assess_new_info",
                            params: { id: rparams.row.Gid }
                          });
                        }  //特困评估
                      else if (rparams.row.AssessInfo.AssessType == 3) {
                        this.$router.push({
                          name: "tekun_assess_info",
                          params: { id: rparams.row.Gid }
                        });
                      }
                      //新特困评估
                      else if (rparams.row.AssessInfo.AssessType == 4) {
                        this.$router.push({
                          name: "tekun_new_assess_info",
                          params: { id: rparams.row.Gid }
                        });
                      }
                    }
                  }
                },
                rparams.row.FinalState
              );
            }
          }
        },
        {
          title: "评估人",
          align: "center",
          width: 80,
          render: (h, params) => {
            if (params.row.AssessInfo != null) {
              return h("div", params.row.AssessInfo.sys_CreaterName);
            }
          }
        },
        {
          title: "是否新增",
          key: "IsNew",
          align: "center",
          width: 80,
          render: (h, params) => {
            if (params.row.IsNew == 1) {
              return h("div", "新增");
            } else {
              return h();
            }
          }
        },
        {
          title: "备注",
          align: "center",
          key: "Remark",
          render: (h, params) => {
            return h("div", [
              h(
                "span",
                {
                  style: "margin-right:10px"
                },
                params.row.Remark
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.personTemp = params.row;
                      this.modalRemark = true;
                    }
                  }
                },
                params.row.Remark ? "编辑" : "新增"
              )
            ]);
          }
        }
      ],
      tableData: [],
      stateList: [
        {
          label: "所有人员",
          value: "-1"
        },
        {
          label: "待评估",
          value: "0"
        },
        {
          label: "已评估",
          value: "1"
        },
        {
          label: "无法评估",
          value: "2"
        }
      ],
      personState: "-1",
      pName: "",
      town: "",
      townList: []
    };
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  watch: {
    personState: function(newQuestion, oldQuestion) {
      this.Search();
    },
    town: function(newQuestion, oldQuestion) {
      this.Search();
    }
    // ,id: function(newQuestion, oldQuestion) {
    //  this.personState="-1";
    //   this.Search();
    // }
  },
  methods: {
    handleEdit(row) {
      if (row.$isEdit) {
        this.$set(row, "$isEdit", false);
      } else {
        this.$set(row, "$isEdit", true);
      }
    },
    handleSave(row, index) {
      if (row.$isEdit) {
        this.$set(row, "$isEdit", false);
      } else {
        this.$set(row, "$isEdit", true);
      }
    },
    SavePerson() {
      let vm = this;
      if (!this.personTemp.Name) {
        Message.error("姓名不能为空");
        return;
      }
      if (!this.personTemp.IdCard) {
        Message.error("身份证号不能为空");
        return;
      }
      if (!this.personTemp.Address) {
        Message.error("住址不能为空");
        return;
      }
      SavePerson(this.personTemp).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          vm.modal2 = false;
          vm.GetGroupTown();
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    SyncEmplyeeSign(){
      SyncEmplyeeSign(this.id).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          this.$Modal.success({ title: "提示", content: '同步完成' });
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    SaveRemark() {
      let vm = this;
      var reqData = {
        gid: this.personTemp.Gid,
        remark: this.personTemp.Remark
      };
      SavePersonRemark(reqData).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          this.Search();
          vm.modalRemark = false;
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    DeletePerson(id) {
      this.$Modal.confirm({
        title: "提示",
        content: "确定要删除此评估人员吗？",
        onOk: () => {
          DeletePerson(id).then(res => {
            const data = res.data;
            if (data.Type == 1) {
              this.Search();
              this.$Modal.success({ title: "提示", content: "删除成功" });
            } else {
              this.$Modal.error({ title: "提示", content: data.Message });
            }
          });
        },
        onCancel: () => {}
      });
    },
    GetGroupTown() {
      let vm = this;
      GetGroupTown(vm.id).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          vm.townList = data.Data;
          var all = { TownName: "所有乡镇" };
          vm.townList.unshift(all);
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    Search() {
      let vm = this;
      var isnew = 0;
      if (vm.isNew) {
        isnew = 1;
      }
      var town = vm.town;
      if (town == "所有乡镇") {
        town = "";
      }
      var reqData = {
        groupid: vm.id,
        grouptown: town,
        state: vm.personState,
        isnew: isnew,
        page: vm.pPage,
        size: vm.pSize,
        str: vm.pName
      };
      SearchPerson(reqData).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          vm.total = data.Data.Total;
          vm.tableData = data.Data.Rows;
        } else {
          Message.error(data.Message);
        }
      });
    },
    SaveActionLog(vaction, vfield) {
      var reqData = {
        Action: vaction,
        Filed: vfield
      };
      SaveActionLog(reqData).then(res => {});
    },
    Export() {
      if (this.tableData.length) {
        const sorted = this.groupBy(this.tableData, function(item) {
          return [item.GroupTown];
        });

        this.exportLoading = true;
        const params = {
          title: [
            "序号",
            "姓名",
            "性别",
            "住址",
            "身份证号",
            "评估结果",
            "是否新增",
            "备注"
          ],
          key: [
            "R_INDEX",
            "Name",
            "Sex",
            "Address",
            "IdCard",
            "FinalState",
            "IsNew",
            "Remark"
          ],
          data: sorted,
          autoWidth: true,
          filename: this.groupname
        };
        excel.export_array_to_excel_more(params);
        this.exportLoading = false;
        this.SaveActionLog("导出", this.groupname);
      } else {
        this.$Message.info("表格数据不能为空！");
      }
    },
    groupBy(array, f) {
      const groups = {};
      array.forEach(function(o) {
        const group = f(o);
        groups[group] = groups[group] || [];
        groups[group].push(o);
      });
      return Object.keys(groups).map(function(group) {
        return groups[group];
      });
    },
    InputPrintTitle() {
      if (!this.pringtTitle) {
        this.pringtTitle = this.groupname;
      }
      this.modal3 = true;
    },
    Print() {
      this.modal3 = false;
      var tableobj = {
        Table: this.tableData
      };
      document
        .getElementById("iframe-print")
        .contentWindow.printData(this.pringtTitle, JSON.stringify(tableobj));
      this.SaveActionLog("打印", this.groupname);
    }
  },
  created() {},
  mounted() {
    var cardHeight = this.$refs.card.$el.offsetHeight;
    this.tableHeight = window.innerHeight - cardHeight - 200;
    this.groupname = this.$route.query.groupname;
    this.GetGroupTown();
    this.Search();
  }
};
</script>
